﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;

namespace DentalSoftware
{
    public partial class ThuChiForm : Form
    {
        public ThuChiForm(int width, int height)
        {
            InitializeComponent();
            this.Height = height;
            this.Width = width;
        }

        private void ThuChiForm_Load(object sender, EventArgs e)
        {
            dtpNgayBD.Value = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
            dtpNgayKT.Value = DateTime.Now;
            Reload();
        }

        private void Reload()
        {            
            List<PhieuThuChiDTO> list = ThuChiBUS.SelectPhieuByNgayDauNgayCuoi(dtpNgayBD.Value,dtpNgayKT.Value);
            dgvThuChi.AutoGenerateColumns = false;
            dgvThuChi.DataSource = list;

            double thu, chi;
            thu = chi = 0;
            foreach (PhieuThuChiDTO ptc in list)
            {
                if (ptc.Loai == 1)
                    thu += ptc.Sotien;
                else
                    chi += ptc.Sotien;
            }
            txtChi.Text = chi.ToString("0,00");
            txtThu.Text = thu.ToString("0,00");
            txtDoanhThu.Text = (thu - chi).ToString("0,00");
        }

        private void btnThu_Click(object sender, EventArgs e)
        {
            PhieuThu thu = new PhieuThu();
            if (thu.ShowDialog() == DialogResult.Yes)
            {
                Reload();
            }
        }

        private void btnChi_Click(object sender, EventArgs e)
        {
            PhieuChi chi = new PhieuChi();
            if (chi.ShowDialog() == DialogResult.Yes)
            {
                Reload();
            }
        }

        private void dtpNgayKT_ValueChanged(object sender, EventArgs e)
        {
            Reload();
        }

        private void dtpNgayBD_ValueChanged(object sender, EventArgs e)
        {
            Reload();
        }
    }
}
